﻿using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web;

namespace SusuCMS.Web
{
    public class PageHtml
    {
        private HtmlHelper htmlHelper;

        public PageHtml(HtmlHelper htmlHelper)
        {
            this.htmlHelper = htmlHelper;
        }

        public MvcHtmlString Partial(string partialViewName, object model = null, ViewDataDictionary viewData = null)
        {
            var website = htmlHelper.PageContext().CurrentWebsite;

            var fileName = string.Format("~/wwwroot/{0}/Views/Shared/{1}.cshtml", website.Identifier, partialViewName);

            return htmlHelper.Partial(fileName, model, viewData);
        }

        public string UrlContent(string contentPath)
        {
            var context = htmlHelper.PageContext();
            var website = context.CurrentWebsite;
            var path = string.Format("/wwwroot/{0}/{1}", website.Identifier, contentPath.TrimStart(new[] { '/' }));

            return path;
        }

        public string Url(string url)
        {
            var context = htmlHelper.PageContext();
            var subDirectory = context.SubDirectory;

            if (string.IsNullOrWhiteSpace(subDirectory))
            {
                return string.Format("/{0}", url.TrimStart(new[] { '/' }));
            }

            return string.Format("/{0}/{1}", subDirectory, url.TrimStart(new[] { '/' }));
        }
    }
}
